package _04Dom4j.XPath;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.util.List;

public class Demo2 {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("day14\\src\\_04Dom4j\\XPath\\xpath_contact.xml");
        Element rootElement = document.getRootElement();

        //查找所有具备id属性的元素
        List<Node> nodes = rootElement.selectNodes("//*[@id]");
        for (Node node : nodes) {
            System.out.println(node.getName());
        }
        System.out.println("================================");
        //查找所有具备id属性的contact元素
        List<Node> nodes2 = rootElement.selectNodes("//contact[@id]");
        for (Node node : nodes2) {
            System.out.println(node.getName());
        }
        System.out.println("================================");
        //查找所有vip属性值为true的contact元素的姓名
        List<Node> nodes3 = rootElement.selectNodes("//contact[@vip='true']");
        for (Node node : nodes3) {
            Node node1 = node.selectSingleNode("./name");
            System.out.println(node1.getText());
        }
        //查找文本值为"李四"的name标签，获取id值
        List<Node> nodes4 = rootElement.selectNodes("//name[text()='李四']");
        for (Node node : nodes4) {

        }
    }
}
